package com.credithc.signature.job.utils;

import com.credithc.signature.common.constant.Constant;
import com.credithc.signature.common.utils.DateUtil;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

/**
 * @desc: <br/>
 * @author: Jingbo Hu<br/>
 * @time：2020年07⽉01⽇<br/>
 * @version：v1.0.0<br>
 */
public class PathUtil {


    /**
     * 获取未签章pdf文件路径
     *
     * @param requestSN
     * @return
     */
    public static String getSnapPDFPath(String snapNoSignPdf, String requestSN) {
        String pdfName = Constant.SNAP_PDF_PREFIX + requestSN + Constant.PDF_EXT;
        String date = DateUtil.getStandardDateStr();
        File snapPDFDictory = new File(snapNoSignPdf + File.separator + date);
        if (!snapPDFDictory.exists()) {
            snapPDFDictory.mkdirs();
        }
        return snapNoSignPdf + File.separator + date + File.separator + pdfName;
    }

    /**
     * 获取要签章pdf文件路径
     *
     * @param snapPDFPath
     * @param fileName
     * @param date
     * @return
     */
    public static String getSignSnapPDFPath(String snapPDFPath, String fileName, String date) {
        File snapPDFDictory = new File(snapPDFPath + File.separator + date);
        if (!snapPDFDictory.exists()) {
            snapPDFDictory.mkdirs();
        }
        return snapPDFPath + File.separator + date + File.separator + fileName;
    }

    /**
     * 复制文件路径
     *
     * @param url1
     * @param url2
     * @throws Exception
     */
    public static void copy(String url1, String url2) throws Exception {
        File file = new File(url1);
        if (file.exists()) {
            FileInputStream in = new FileInputStream(new File(url1));
            FileOutputStream out = new FileOutputStream(new File(url2));
            byte[] buff = new byte[512];
            int n = 0;
            System.out.println("复制文件：\n源路径：" + url1 + "\n" + "目标路径：" + url2);
            while ((n = in.read(buff)) != -1) {
                out.write(buff, 0, n);
            }
            out.flush();
            in.close();
            out.close();
            System.out.println("复制完成");
        } else {
            System.out.println("源文件不存在");
        }
    }
}
